package com.vgulu.server.common.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RequestUtil {
	

	public static Date parseDate(HttpServletRequest request, String name) {
		return parseDate(request, name, null);
	}
	
	public static Date parseDate(HttpServletRequest request, String name, Date def) {
		String dateStr = request.getParameter(name);
		try {
			
			if(dateStr == null || dateStr.length() == 0){
				return def;
			}
			if (dateStr.length() == 10) {
				return new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
			}
			if (dateStr.length() == 19) {
				return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
			}
		} catch (ParseException ex) {
			throw new IllegalArgumentException(
					dateStr + "is not an valid format for: [yyyy-MM-dd] or [yyyy-MM-dd HH:mm:ss]");
		}
		throw new IllegalArgumentException(dateStr + "is not an valid format for: [yyyy-MM-dd] or [yyyy-MM-dd HH:mm:ss]");
	}


	public static String param(HttpServletRequest request, String name) {
		String result = request.getParameter(name);
		return result == null ? result : StringUtils.trim(result);
	}

	public static String param(HttpServletRequest request, String name, String def) {
		String v = request.getParameter(name);
		return v == null ? def : StringUtils.trim(v);
	}
    
	public static Integer paramInteger(HttpServletRequest request, String name){
		return paramInteger(request, name, null);
	}
	
	public static Integer paramInteger(HttpServletRequest request, String name, Integer def){
		String v = request.getParameter(name);
		if (v == null || v.length() == 0) {
			return def;
		}
		try {
			return Integer.valueOf(v);
		} catch (NumberFormatException ex) {
			return def;
		}
	}
	
	
	public static int paramInt(HttpServletRequest request, String name) {
		return paramInt(request, name, 0);
	}

	public static int paramInt(HttpServletRequest request, String name, int def) {
		String v = request.getParameter(name);
		if (v == null || v.length() == 0) {
			return def;
		}
		try {
			return Integer.parseInt(v);
		} catch (NumberFormatException ex) {
			return def;
		}
	}

	public static long paramLong(HttpServletRequest request, String name) {
		return paramLong(request, name, 0);
	}

	public static long paramLong(HttpServletRequest request, String name, long def) {
		String v = request.getParameter(name);
		if (v == null || v.length() == 0) {
			return def;
		}
		try {
			return Long.parseLong(request.getParameter(name));
		} catch (NumberFormatException ex) {
			return def;
		}
	}

	public static double paramDouble(HttpServletRequest request, String name) {
		return paramDouble(request, name, 0.0);
	}
	
	
	public static Double paramDoubleClz(HttpServletRequest request, String name){
		return paramDoubleClz(request, name, null);
	}
	
	
	public static Double paramDoubleClz(HttpServletRequest request, String name, Double def) {
		String v = request.getParameter(name);
		if (v == null || v.length() == 0) {
			return def;
		}
		return Double.valueOf(v);
	}
	
	public static double paramDouble(HttpServletRequest request, String name, double def) {
		String v = request.getParameter(name);
		if (v == null || v.length() == 0) {
			return def;
		}
		try {
			return Double.parseDouble(request.getParameter(name));
		} catch (NumberFormatException ex) {
			return def;
		}
	}

	public static String cookie(HttpServletRequest request, String cookieName) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie c : cookies) {
				if (c.getName().equals(cookieName)) {
					return c.getValue();
				}
			}
		}
		return null;
	}


	public static byte[] body(HttpServletRequest request) throws IOException {
		InputStream in = request.getInputStream();
		ByteArrayOutputStream out = new ByteArrayOutputStream(2*1024);
		byte[] buf = new byte[1024];
		int len = 0;
		while ((len = in.read(buf)) != -1) {
			out.write(buf,0,len);
		}
		in.close();
		byte[] ret = out.toByteArray();
		out.close();
		return ret;
	}

}
